function add_row()
{
        if(check_value())
        {                
                var dl = document.createElement('dl');
                dl.setAttribute('class','fin-title fin-form2'); 
                dl.setAttribute('className','fin-title fin-form2');
                dl.setAttribute('id','item'+i); 
                
                
                var dd = document.createElement('dd');
                var account_id = document.getElementById('account_id0').value;
                dd.innerHTML = '<input type="hidden" name="accountId" value="'+account_id+'"/><a href="/bin/finance.py/show_account_details?a='+account_id+'">'+account_id+'</a>';
                dl.appendChild(dd);
                
                var dd = document.createElement('dd');
                var dr_amount = document.getElementById('dr_amount0').value;
                var DRamount = 0; 
                if(dr_amount!=''){
                        DRamount += parseFloat(dr_amount);
                } 
                if(DRamount == 0){
                        dd.innerHTML = '<input type="hidden" name="DRamount" value=""/>&nbsp;';
                }else{
                        dd.innerHTML = '<input type="hidden" name="DRamount" value="'+DRamount+'"/>'+DRamount;
                }     
                dl.appendChild(dd);
                
                var dd = document.createElement('dd');
                var cr_amount = document.getElementById('cr_amount0').value;
                var CRamount = 0;                
                if(cr_amount!=''){
                        CRamount += parseFloat(cr_amount);
                }
                if(CRamount == 0){
                        dd.innerHTML = '<input type="hidden" name="CRamount" value=""/>&nbsp;';
                }else{
                        dd.innerHTML = '<input type="hidden" name="CRamount" value="'+CRamount+'"/>'+CRamount;
                }   
                dl.appendChild(dd);
                
                var dd = document.createElement('dd');
                var description = document.getElementById('description0').value;
                dd.innerHTML = '<div style="display:none"><textarea name="description">'+description+'</textarea></div>'+escapeHTML(description);
                dl.appendChild(dd);
                
                var dt = document.createElement('dt');             
                dt.innerHTML = '<a href="javascript:void(0)" onclick="del_row(\'item'+i+'\');document.getElementById(\'item'+i+'\').style.display=\'none\';check_balance(sumOldDrValue,sumOldCrValue,'+DRamount+','+CRamount+',0);">Delete</a>';
                dl.appendChild(dt);
                obj.appendChild(dl);
                i += 1; 
                clean_up();
                check_balance(sumOldDrValue,sumOldCrValue,DRamount,CRamount,1);
        }
}

function del_row(id){
    del_Tr = document.getElementById(id);
    len = del_Tr.childNodes.length;
    for(j=0;j<len;j++){
            del_Tr.childNodes[j].innerHTML='';
    }
    del_Tr.style.display='none';
}
function check_balance(num1,num2,num3,num4,flag)
{      
        var old_total_DR = parseFloat(num1);
        var old_total_CR = parseFloat(num2);
        
        var new_total_DR = 0;        
        var new_total_CR = 0;
        
        var del_DR = 0;
        var del_CR = 0; 
        if(num3==""){
                del_DR += 0;
        }else{
                del_DR += parseFloat(num3);
        }
       
        if(num4==""){
                del_CR += 0;
        }else{
                del_CR += parseFloat(num4);
        }        
        
        if(flag==0){
                sumOldDrValue = old_total_DR + new_total_DR - del_DR;
                sumOldCrValue = old_total_CR + new_total_CR - del_CR;
        }
        if(flag==1){
                sumOldDrValue = old_total_DR + new_total_DR + del_DR;
                sumOldCrValue = old_total_CR + new_total_CR + del_CR;
        }
        if(flag==2){
                if(sumOldDrValue == sumOldCrValue&&sumOldDrValue!=0){
                        return true;
                }
                else{
                        return false;
                }
        }
}

function check_value()
{
        var account_id = document.getElementById('account_id0').value;
        var dr_amount = document.getElementById('dr_amount0').value;
        var cr_amount = document.getElementById('cr_amount0').value;
        var description = document.getElementById('description0').value;        
        var bool = true;
        
        if(account_id==""||(dr_amount==""&&cr_amount=="")||description=="")
        {               
                bool = false;
        }
        if(dr_amount!=""&&cr_amount!="")
        {
                document.getElementById('cr_amount0').value = "";
                document.getElementById('dr_amount0').value = "";
                bool = false;
        }  
        return bool;
}
function clean_up()  
{   
        document.getElementById('account_id0').value='';
        document.getElementById('dr_amount0').value='';
        document.getElementById('cr_amount0').value='';
        document.getElementById('description0').value='';        
}
function submit_form(id){
        form=document.getElementById(id);
        form.action="/bin/finance.py/edit_journal_complete";
        if(check_balance(sumOldDrValue,sumOldCrValue,0,0,2)){
                form.submit();
        }else{
                alert('Not balance!');
                return;
        }        
}
function disabled_obj(){
		var obj1 = document.getElementById('dr_amount0');
		var obj2 = document.getElementById('cr_amount0');
		if(obj1.value.trim().length==0 && obj2.value.trim().length==0){				
				//obj1.disabled=null;
				//obj2.disabled=null;
				obj1.style.display='block';
				obj2.style.display='block';
				return;
		}
		if(obj1.value.trim().length>0){
				obj2.style.display='none';
				//obj2.disabled='disabled';
				return;
		}
		if(obj2.value.trim().length>0){
				//obj1.disabled='disabled';
				obj1.style.display='none';
				return;
		}   
}
var i=0;
var sumOldDrValue=0;
var sumOldCrValue=0;
function init(){
		{% if journal_items %}
		{% for item in journal_items %}     
				var dl = document.createElement('dl');
				dl.setAttribute('class','fin-title fin-form2'); 
				dl.setAttribute('className','fin-title fin-form2');        
				dl.setAttribute('id','item'+i);
				
				var dd = document.createElement('dd');      
				dd.innerHTML = '<input type="hidden" name="old_accountId" value = "{{item['id']}}" /><a href="/bin/finance.py/show_account_details?a={{item['account_id']}}">{{item['account_id']}}</a>';
				dl.appendChild(dd);
				
				{% if item['drcr'] == 'DR' %}
						sumOldDrValue += parseFloat({{item['amount']}});
						var dd = document.createElement('dd'); 
						dd.innerHTML= "{{item['amount']}}";
						dl.appendChild(dd);                        
		
						var dd = document.createElement('dd');
						dd.innerHTML= "&nbsp;";                  
						dl.appendChild(dd);
						
						var dd = document.createElement('dd'); 
						dd.innerHTML= "{{item['description']}}";
						dl.appendChild(dd);
						
						var dt = document.createElement('dt'); 
						dt.innerHTML= '<a href="javascript:void(0)" onclick="del_row(\'item'+i+'\');document.getElementById(\'item'+i+'\').style.display=\'none\';check_balance(sumOldDrValue,sumOldCrValue,{{item['amount']}},0,0);">Delete</a>';
						dl.appendChild(dt);
				{% else %}
						sumOldCrValue += parseFloat({{item['amount']}});
						var dd = document.createElement('dd'); 
						dd.innerHTML= "&nbsp;";
						dl.appendChild(dd);                        
		
						var dd = document.createElement('dd');
						dd.innerHTML= "{{item['amount']}}";                  
						dl.appendChild(dd); 
						
						var dd = document.createElement('dd'); 
						dd.innerHTML= "{{item['description']}}";
						dl.appendChild(dd);
						
						var dt = document.createElement('dt'); 
						dt.innerHTML= '<a href="javascript:void(0)" onclick="del_row(\'item'+i+'\');document.getElementById(\'item'+i+'\').style.display=\'none\';check_balance(sumOldDrValue,sumOldCrValue,0,{{item['amount']}},0);">Delete</a>';
						dl.appendChild(dt);
				{% endif %}
				obj.appendChild(dl);
				i +=1;
		{% endfor %}
		{% endif %}
}
